package com.trnnn.strutslit.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.trnnn.strutslit.ActionController;
import com.trnnn.strutslit.Debug;

public class DispatchAndExecuteFilter implements Filter {

	private ActionController controller;

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) arg0;
		HttpServletResponse response = (HttpServletResponse) arg1;

		boolean isStatic = isStaticResourceReqeuest(request, response);
		System.out.println("------"+isStatic);
		if (isStatic) {
			chain.doFilter(arg0, arg1);
			return;
		}
		controller.doAction(request,response);
	}

	private boolean isStaticResourceReqeuest(HttpServletRequest request,
			HttpServletResponse response) {
		String requestUri=request.getRequestURI();
		Debug.output("[DispatchAndExecuteFilter]isStaticResourceReqeuest requestUri->>"+requestUri);
		return StaticResourceFilter.isStaticResource(requestUri);
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		Debug.open();
		Debug.output("Strutslit is starting....");
		controller = new ActionController(config);
		ActionCollector.init();
	}

}
